package com.baselibrary.base.mvvm

import androidx.lifecycle.MutableLiveData
import com.baselibrary.MessageBus
import com.baselibrary.UserInfo
import com.baselibrary.bean.*
import com.baselibrary.extension.toast
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.net.api.WineCoinsApi
import com.baselibrary.utils.EventBusUtils
import com.baselibrary.utils.SpUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class BaseRequestManager {
    companion object {
        @Volatile
        private var instance: BaseRequestManager? = null

        fun getInstance(): BaseRequestManager {
            return instance ?: synchronized(this) {
                instance ?: BaseRequestManager().also { instance = it }
            }
        }
    }

    /**
     * @param type    1用户 2话题 3专栏 4讲坛
     * @param thirdId id
     */
    fun reqAddFollow(mutableLiveData: MutableLiveData<FollowSuccBean>, type: Int, thirdId: Int) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqAddFollow2(it, type, thirdId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<FollowSuccBean>(this) {
                override fun onSuccess(t: FollowSuccBean, json: String) {
                    "关注成功".toast()
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = null
                }
            })
        }
    }

    fun reqCancelFollow(mutableLiveData: MutableLiveData<FollowSuccBean>, type: Int, thirdId: Int) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqCancelFollow2(it, type, thirdId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<FollowSuccBean>(this) {
                override fun onSuccess(t: FollowSuccBean, json: String) {
                    "已取消关注".toast()
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = null
                }
            })
        }
    }

    /**
     * @param type   类型：1文章 2产品 3课程 4动态 5讲坛 6专栏 7约局
     * @param typeId 收藏内容的id
     */
    fun reqAddCollection(mutableLiveData: MutableLiveData<Boolean>, type: Int, typeId: Long) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqAddCollection(it, type, typeId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(t: Boolean, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = false
                }
            })
        }
    }

    /**取消收藏
     *  type"类型：1文章 2产品 3课程 4动态 5讲坛 6专栏 7约局
     * @author: merbng
     * @time: 2022/4/28 11:22
     * @description:
     */
    fun reqCancelCollection(mutableLiveData: MutableLiveData<Boolean>, type: Int, typeId: Long) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqCancelCollection(it, type, typeId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(t: Boolean, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = false
                }
            })
        }
    }

    /**
     * * @param type   点赞类型 1评论的点赞 2文章点赞 3点评点赞 4动态  6课程 7直播间
     */
    fun reqAddSupport(mutableLiveData: MutableLiveData<Boolean>, type: Int, typeId: Int) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqAddSupport(it, type, typeId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(t: Boolean, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = false
                }
            })
        }
    }

    fun reqCancelSupport(mutableLiveData: MutableLiveData<Boolean>, type: Int, typeId: Int) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqCancelSupport(it, type, typeId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(t: Boolean, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = false
                }
            })
        }
    }

    //好友列表
    fun reqFriendList(keyWord: String, mutableLiveData: MutableLiveData<List<FriendListBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            if (!SpUtils.getInstance().isLogin) return@let
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqFriendList(keyWord, it).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<List<FriendListBean>>(this) {
                override fun onSuccess(t: List<FriendListBean>, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = null
                }
            })
        }
    }

    //删除动态
    fun deleteTimeline(mutableLiveData: MutableLiveData<String>, tid: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).deleteTimeline(it, tid, "1").subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<String>(this) {
                override fun onSuccess(t: String, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = null
                }
            })
        }
    }


    //奖励酒币
    fun reqGiveCoin(mutableLiveData: MutableLiveData<Int>, type: Int) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineCoinsApi::class.java).getWineCoins(it, type).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Int>(this) {
                override fun onSuccess(t: Int, json: String) {
                    mutableLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    mutableLiveData.value = null
                }
            })
        }
    }

    /*获取首页瀑布流*/
    fun requestStream(type: Int, listData: MutableLiveData<StreamData.DataDTO>, page: String, cursor: String) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestStream(SpUtils.getInstance().loadUserBean?.data?.token, type, page, cursor)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<StreamData.DataDTO>(this) {
            override fun onSuccess(t: StreamData.DataDTO, json: String) {
                if (t != null) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /*频道瀑布流*/
    fun requestChannel(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, channelId: Int, page: String, cursor: String) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestChannelTimeline(SpUtils.getInstance().loadUserBean?.data?.token, channelId, page, cursor)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
            override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                if (t != null) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    fun requestHotAndLatest(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, page: String, tid: String, type: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestHotAndLatest(it, page, "10", tid, type)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
                override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                    if (t != null) listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    fun requestUserTimeline(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, page: String, uid: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestUserTimeline(it, page, "20", uid)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
                override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                    if (t != null) listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    @Deprecated("目前都使用用户动态")
    fun requestWineryTimeline(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, page: String, wineryCode: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestWineryTimeline(it, page, "20", wineryCode)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
                override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                    if (t != null) listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    /**
     * 更新用户信息
     */
    fun reqUpUserInfo(apiResponse: APIResponse<UserInfo.DataBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqUpUserInfo(it).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(apiResponse)
        }
    }

    fun requestUserCollection(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, page: String, type: String, userId: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestUserCollection(it, page, "10", type, userId)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
                override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                    if (t != null) listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    fun requestUserLiked(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, page: String, userId: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestUserLiked(it, page, "10", userId)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
                override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                    if (t != null) listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    fun requestSearchTopic(listData: MutableLiveData<DynamicInfoBean.DataBeanX>, page: String, tid: String, keyword: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).requestSearchTopic(it, page, "10", tid, keyword)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<DynamicInfoBean.DataBeanX>(this) {
                override fun onSuccess(t: DynamicInfoBean.DataBeanX, json: String) {
                    if (t != null) listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    fun reqAddShareNum(id: Long, listData: MutableLiveData<Any>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqAddShareNum(it, id).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
                override fun onSuccess(t: Any, json: String) {
                    listData.value = t
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    /**秒杀--添加预约
     * @author: merbng
     * @time: 2023/3/20 13:40
     * @description:
     */
    fun reqSeckillAddSubscribe(listData: MutableLiveData<SeckillAddSubscribeBean>, actId: String) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqSeckillAddSubscribe(actId)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<SeckillAddSubscribeBean>(this) {
            override fun onSuccess(t: SeckillAddSubscribeBean, json: String) {
                EventBusUtils.sendNotify(MessageBus.msgId_refresh_seckill_sub_status)
                if (t != null) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**秒杀--取消预约
     * @author: merbng
     * @time: 2023/3/20 13:41
     * @description:
     */
    fun reqSeckillCancelSubscribe(mutableLiveData: MutableLiveData<Any>, actId: String) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqSeckillCancelSubscribe(actId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                EventBusUtils.sendNotify(MessageBus.msgId_refresh_seckill_sub_status)
                mutableLiveData.value = t
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                mutableLiveData.value = false
            }
        })
    }

    /**根据类型获取banner列表（首页大改版）
     * @time: 2023/4/20 11:48
     * @description:    bannerType： banner类型 0首页banner 1商城banner 2附近banner 3中心banner 4话题轮播图
     */
    fun reqGetBannerConfigList(bannerType: Int,otherId:Long=-1, mutableLiveData: MutableLiveData<BannerConfigListBean>) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqGetBannerConfigList(bannerType,otherId).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<BannerConfigListBean>(this) {
            override fun onSuccess(t: BannerConfigListBean, json: String) {
                mutableLiveData.value = t
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                mutableLiveData.value = null
            }
        })
    }

    /**获取热词列表（首页大改版）
     * @author: merbng
     * @time: 2023/4/20 11:48
     * @description:areaType： 位置0首页 1商城
     */
    fun reqGetCarouselHotWordList(areaType: Int, mutableLiveData: MutableLiveData<List<HotWordListBean>>) {
        MyHttpUtil.getInstance().getApi(BaseApi::class.java).reqGetCarouselHotWordList(areaType).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<List<HotWordListBean>>(this) {
            override fun onSuccess(t: List<HotWordListBean>, json: String) {
                mutableLiveData.value = t
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                mutableLiveData.value = emptyList()
            }
        })
    }
}